package com.zhilei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zhilei.base.BaseInfoProperties;
import com.zhilei.enums.MsgReveiverTypeEnum;
import com.zhilei.enums.UserRole;
import com.zhilei.exceptions.GraceException;
import com.zhilei.grace.result.ResponseStatusEnum;
import com.zhilei.intercept.JWTCurrentUserInterceptor;
import com.zhilei.mapper.UsersMapper;
import com.zhilei.pojo.Users;
import com.zhilei.pojo.bo.ModifyUserBO;
import com.zhilei.service.UsersService;
import com.zhilei.untils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 志磊
 * @since 2024-09-20
 */
@Service
public class UsersServiceImpl extends BaseInfoProperties implements UsersService {

    @Autowired
    private UsersMapper usersMapper;


    /**
     * 用户修改个人信息
     *
     * @param mod
     */
    @Transactional
    @Override
    public void ModifyUserInfo(ModifyUserBO mod) {
        // TODO: 2024/4/8 实现修改个人信息的业务
        // TODO 修改用户信息校验用户是否存在
        String userId = mod.getUserId();
        if (StringUtils.isBlank(userId)) {
            GraceException.disPlay(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        Users users = new Users();
        users.setId(userId);
        users.setUpdatedTime(LocalDateTime.now());
        BeanUtils.copyProperties(mod, users);
        // TODO: 2024/4/8 实现修改用户信息的持久化
        usersMapper.updateById(users);
    }

    /**
     * 根据用户id，查询用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public Users getUserInfo(String userId) {
        // TODO: 2024/4/8 实现根据用户id，查询用户信息的查询
        Users users = usersMapper.selectById(userId);
        return users;
    }

    /**
     * 根据企业id查询当前企业下有多少hr
     *
     * @param companyId
     * @return
     */
    @Override
    public Long getCountsByCompanyId(String companyId) {
        // TODO: 2024/9/27 实现根据企业id查询当前企业下有多少hr的查询
        Long hrCounts = usersMapper.selectCount(new QueryWrapper<Users>()
                .eq("hr_in_which_company_id", companyId));
        return hrCounts;
    }

    /**
     * 更新角色为HR权限
     *
     * @param hrUserId
     */
    @Override
    public void updateHRRole(String hrUserId) {
        Users users = new Users();
        users.setId(hrUserId);
        users.setRole(MsgReveiverTypeEnum.HR.type);
        users.setUpdatedTime(LocalDateTime.now());
        usersMapper.updateById(users);
    }

    /**
     * 查询HR信息
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PagedGridResult getHrList(Integer page, Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        PageHelper.startPage(page, limit);

        // 在本地线程获取当前登录用户
        Users users = JWTCurrentUserInterceptor.USER_THREAD.get();
        // 成为HR后，认证的（绑定的）公司主键id
        String hrInWhichCompanyId = users.getHrInWhichCompanyId();
        // 根据公司主键查询当前用户
        List<Users> hrInWhichCompanyIdList = usersMapper.selectList(new QueryWrapper<Users>()
                .eq("hr_in_which_company_id", hrInWhichCompanyId)
                .orderByDesc("updated_time"));

        return setterPagedGrid(hrInWhichCompanyIdList, page);
    }

    /**
     * 跟新用户的权限
     *
     * @param hrUserId
     */
    @Transactional
    @Override
    public void uploadUserToHr(String hrUserId) {

        Users users = usersMapper.selectById(hrUserId);
        users.setRole(UserRole.RECRUITER.type);
        users.setUpdatedTime(LocalDateTime.now());

        usersMapper.updateById(users);
    }

    /**
     * 离职解绑员工信息
     *
     * @param hrUserId 用户的id
     */
    @Override
    public void changeUserToCand(String hrUserId) {
        Users users = usersMapper.selectById(hrUserId);
        users.setHrInWhichCompanyId("0");
        users.setRole(UserRole.CANDIDATE.type);
        users.setUpdatedTime(LocalDateTime.now());
        usersMapper.updateById(users);
    }

    /**
     * 绑定hr跟企业之间的关系
     *
     * @param hrUserId
     * @param realname
     * @param companyId
     */
    @Override
    public void bindingHRToCompany(String hrUserId, String realname, String companyId) {
        Users users = new Users();
        users.setId(hrUserId);
        users.setRealName(realname);
        users.setHrInWhichCompanyId(companyId);
        users.setUpdatedTime(LocalDateTime.now());
        usersMapper.updateById(users);
    }
}

































